package com.htudata.core.util.operation;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringOperationUtils {

  public static final String WRAP = "\r\n";
  
  public static final String TAB = "\t";

  public static boolean isValidStr(String str) {
    if (str != null) {
      str = str.trim();
      if (str.length() > 0)
        return true; 
    } 
    return false;
  }
  
  public static boolean isValidStr2(String str) {
    if (isValidStr(str) && 
      !"null".equals(str))
      return true; 
    return false;
  }
  
  public static String getValueOrReturnDefaultValue(String valSrc, String defaultValue) {
    String result = valSrc;
    if (!isValidStr(result))
      return defaultValue; 
    return result;
  }
  
  public static String getStrRange(List<String> strList, String tempStr) {
    if (!CollectionsOperationUtils.isValidCollection(strList))
      return ""; 
    StringBuffer result = new StringBuffer();
    for (String str : strList)
      result.append(String.valueOf(tempStr) + str + tempStr + ","); 
    StringBufferOperationUtils.replaceLastChar(result, "");
    return result.toString();
  }
  
  public static String removeLastChar(String str) {
    String result = str.substring(0, str.length() - 1);
    return result;
  }
  
  public static String removeChinese(String str) {
    String result = str.replaceAll("[\\u4e00-\\u9fa5]", "");
    return result;
  }
  
  public static boolean isPositiveInt(String str) {
    return RegExpOperationUtils.matches(str, "[1-9]\\d*");
  }
  
  public static boolean isUINT(String str) {
    return RegExpOperationUtils.matches(str, "[1-9]\\d*|0");
  }
  
  public static boolean isUINTAndNegativeInt(String str) {
    return RegExpOperationUtils.matches(str, "-?[1-9]\\d*|0");
  }
  
  public static boolean isNumeric(String str) {
    return RegExpOperationUtils.matches(str, "((-?[1-9]\\d*\\.?\\d*(?<!\\.)$)|(-?0\\.\\d*(?<!\\.)$)|0)");
  }
  
  public static boolean HasDigit(String content) {
    boolean result = false;
    Pattern p = Pattern.compile(".*\\d+.*");
    Matcher m = p.matcher(content);
    if (m.matches())
      result = true; 
    return result;
  }
  
  public static String substringDecimal(String val, int decimalPlace) {
    String result = val;
    int index = result.indexOf(".");
    if (index == -1)
      return result; 
    int index2 = index + decimalPlace + 1;
    result = result.substring(0, index2);
    return result;
  }
  
  public static String getStringAddition(String strSrc, int strResultLength, String strAddition, String strAdditionPosision) {
    String result = "";
    result = String.format("%" + strAddition + strResultLength + "d", new Object[] { Integer.valueOf(Integer.parseInt(strSrc)) });
    return result;
  }
  
  public static String rvZeroAndDot(String strSrc) {
    String result = strSrc;
    if (!isValidStr(result))
      return result; 
    if (result.indexOf(".") > 0) {
      result = result.replaceAll("0+?$", "");
      result = result.replaceAll("[.]$", "");
    } 
    return result;
  }
  
  public static String getChangeValue(String value) {
    String result = "0";
    if (!isNumeric(value))
      return result; 
    double valueD = Double.parseDouble(value);
    if (valueD > 0.0D)
      result = "+" + value; 
    return result;
  }
}
